package org.sky.sys.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.Logger;
import org.sky.exception.ServiceException;
import org.sky.sys.entity.SysWidget;
import org.sky.sys.mapper.SysWidgetMapper;
import org.sky.sys.mapper.SysCommonMapper;
import org.sky.sys.util.BspUtils;
import org.sky.sys.util.page.PageListData;
import org.sky.sys.util.page.PageListDataUtils;
import org.sky.util.LogUtils;
import org.sky.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weifx
 * @Title: SysWidgetService
 * @ProjectName platform-core
 * @Description: TODO{微件业务类}
 * @date 2019-3-14 16:58:30
 * @Version 1.0
 */
@Service
public class SysWidgetService {
    private final static Logger log = LogUtils.getLogger(SysWidgetService.class);
    @Autowired
    private SysWidgetMapper sysWidgetMapper;
    @Autowired
    private SysCommonMapper sysCommonMapper;

    /**
     * 微件分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param example
     * @return
     */
    public PageListData selectSysWidgetByPage(int pageNum, int pageSize, Example example) {
        PageHelper.startPage(pageNum, pageSize);
        final PageInfo<SysWidget> pageInfo = new PageInfo<SysWidget>(sysWidgetMapper.selectByExample(example));
        return PageListDataUtils.getPageListData().setTotal(pageInfo.getTotal()).setRows(pageInfo.getList());
    }

    /**
     * 保存{新增/修改}微件
     *
     * @param sysWidget
     * @throws ServiceException
     */
    public void saveAddEditSysWidget(SysWidget sysWidget) throws ServiceException {
        try {
            Timestamp ts = sysCommonMapper.queryTimestamp();
            if (null == sysWidget.getId()) {
                Long id = SnowflakeIdWorker.getInstance().nextId();
                sysWidget.setId(id);
                sysWidget.setCreater(BspUtils.getLoginUser().getCode());
                sysWidget.setCreateTime(ts);
                sysWidget.setUpdater(BspUtils.getLoginUser().getCode());
                sysWidget.setUpdateTime(ts);
                sysWidgetMapper.insertSelective(sysWidget);
            } else {
                sysWidget.setUpdater(BspUtils.getLoginUser().getCode());
                sysWidget.setUpdateTime(ts);
                Example e = new Example(SysWidget.class);
                e.createCriteria().andEqualTo("id", sysWidget.getId());
                sysWidgetMapper.updateByExampleSelective(sysWidget, e);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("微件保存失败:{}", e.getMessage());
            throw new ServiceException("微件保存失败:" + e.getMessage(), e);
        }
    }

    /**
     * 根据ID删除微件
     *
     * @param list
     */
    public void delSysWidgetById(List<SysWidget> list) {
        try {
            List idList = new ArrayList();
            for (SysWidget sysWidget : list) {
                idList.add(sysWidget.getId());
            }
            Example example = new Example(SysWidget.class);
            example.createCriteria().andIn("id", idList);
            sysWidgetMapper.deleteByExample(example);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("微件删除失败:{}", e.getMessage());
            throw new ServiceException("微件删除失败:" + e.getMessage(), e);
        }
    }

    /**
     * 根据Id查询微件
     *
     * @param id
     * @return
     */
    public SysWidget getSysWidgetById(Long id) {
        Example example = new Example(SysWidget.class);
        example.createCriteria().andEqualTo("id", id);
        return sysWidgetMapper.selectOneByExample(example);
    }

    /**
     * 查询用户具有的微件
     *
     * @param userCode
     * @return
     */
    public List<SysWidget> selectSysWidgetListByUserCode(String userCode) {
        return sysWidgetMapper.selectSysWidgetListByUserCode(userCode);
    }
}
